﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace TVN.ShopMan.ApplicationInterface
{
    using TVN.ShopMan.ApplicationInterface.AppForms;

    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Gets a child form in the children list, auto create new form if not presented.
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private Form GetChildForm(Type type)
        {
            Form childForm = null;
            foreach (Form formItem in this.MdiChildren)
            {
                if (formItem.GetType() == type)
                {
                    childForm = formItem;
                }
                else
                {
                    formItem.Close();
                    formItem.Dispose();
                }
            }

            if (childForm == null)
            {
                childForm = (Form)Activator.CreateInstance(type);
            }

            childForm.MdiParent = this;
            childForm.Width = this.Width;
            childForm.Height = this.Height;

            return childForm;
        }

        private void ShowAndFocusForm(Type formType)
        {
            Form accountForm = this.GetChildForm(formType);
            accountForm.Show();
            accountForm.Focus();
        }

        private void accountsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.ShowAndFocusForm(typeof(AccountForm));
        }

        private void unitsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.ShowAndFocusForm(typeof(UnitForm));
        }

        private void repositoryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.ShowAndFocusForm(typeof(RepositoryForm));
        }

        private void suppliersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.ShowAndFocusForm(typeof(SupplierForm));
        }

        private void miscToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.ShowAndFocusForm(typeof(GenericDataForm));
        }

        private void productsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.ShowAndFocusForm(typeof(ProductForm));
        }

        private void restartToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Restart();
        }

        private void importToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.ShowAndFocusForm(typeof(ProductImportForm));
        }

        private void MainForm_Shown(object sender, EventArgs e)
        {
            object[] versionAttributes = Assembly.GetExecutingAssembly().GetCustomAttributes(false);
            if (versionAttributes.Length > 0)
            {
                var fileVersionAttr = versionAttributes.FirstOrDefault(attrItem => attrItem.GetType().Equals(typeof(AssemblyFileVersionAttribute)));
                if (fileVersionAttr != null)
                {
                    this.tssVersion.Text = string.Format(this.tssVersion.Text, ((AssemblyFileVersionAttribute)fileVersionAttr).Version);
                }

                var productAttr = versionAttributes.FirstOrDefault(attrItem => attrItem.GetType().Equals(typeof(AssemblyProductAttribute)));
                if (productAttr != null)
                {
                    this.tssProduct.Text = ((AssemblyProductAttribute)productAttr).Product;
                }
            }
        }

    }
}
